/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simulation;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author haoliang.donghl
 */
public class Main {

    private class UpdateLoad extends Thread{
        @Override
        public void run(){
            float t = 0.0f;
            while (t < 1.0f){
                try {
                    LoadReader.getInstance().updateAll(t);
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                } finally{
                    t += 0.005f;
                }
            }
        }
    }

    private class CallAlgorithm extends Thread{
        @Override
        public void run(){
            while (true){
                try {
                    Thread.sleep(500);
                    java.awt.Canvas canvas = JMVisio.getInstance().canvas;
                    canvas.update(canvas.getGraphics());
                } catch (InterruptedException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private class updateGraphic extends Thread{
        @Override
        public void run(){
            while (true){
                try {
                    Thread.sleep(500);
                    SocketMessage.getInstance().connectAlgorithm();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void run(){
        SocketMessage.getInstance();
        LoadReader.getInstance().loadAll();
        LoadReader.getInstance().updateAll(0.0f);
        Thread t1 = new UpdateLoad();
        Thread t2 = new CallAlgorithm();
        Thread t3 = new updateGraphic();
        t2.setDaemon(true);
        t3.setDaemon(true);
        t1.start();
        t2.start();
        t3.start();
    }

    public static void main(String[] args) {
        Main m = new Main();
        m.run();
    }
}
